package aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectjAnnotation {
	//前置通知
	@Pointcut("execution(* daoImpl.*.*(..))")
	private void myPointCut1() {}
	@Before("myPointCut1()")
	public void myBefore(JoinPoint joinPoint) {
		System.out.println("开启事务");
	}
	//后置通知
	@AfterReturning("myPointCut1()")
	public void myAfterReturning(JoinPoint joinPoint) {
		System.out.println("结束事务");
	}
	@Around("myPointCut1()")
	public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
		//开始
		System.out.println("环绕开启事务");
		//执行当前目标方法
		Object obj = proceedingJoinPoint.proceed();
		//结束
		System.out.println("环绕结束事务");
		return obj;
	}
	//异常通知
	@AfterThrowing(value="myPointCut1()",throwing="e")
	public void myAfterThrowing(JoinPoint joinPoint,Throwable e) {
		System.out.println("异常通知:"+"出错了"+e.getMessage());
		System.out.println("进行回滚操作");
	}
}
